<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\gs_developer\model;

use app\common\model\BaseModel;
use Throwable;

class ExceptionModel extends DevBaseModel
{
    protected $name = self::NAME_PREFIX . 'exception';

    protected $type = array(
        'request' => 'json',
        'trace'   => 'json',
    );

    public function setIpAttr($value)
    {
        return ip2long($value);
    }

    public function getIpAttr($value)
    {
        return long2ip($value);
    }

    // 写入前执行
    public static function onBeforeInsert($row)
    {
        $row->request = request()->param();
        $row->ip = request()->ip();
        $row->addon = get_addon();
        $row->plaid = get_plaid();
        $row->client = request()->header('user-agent');
    }

    /**
     * @param $e Throwable
     * @return void
     */
    public function crateException($e)
    {
        $data = array(
            'message' => $e->getMessage(),
            'code'    => $e->getCode(),
            'line'    => $e->getLine(),
            'file'    => str_replace(app()->getRootPath(), "/", $e->getFile()),
            'trace'   => array_slice($e->getTrace(), 0, 10),
        );
        $this->save($data);
    }
}
